package pl.myszon.wifimap;

import com.google.android.maps.GeoPoint;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

class LocationProvider implements LocationListener{
	private Location currentLocation = null;
	private LocationManager locationManager;
	private IUserLocationUpdater updateListener;
	
	private GeoPoint getMyLocation(){
			if(currentLocation == null)
				return new GeoPoint(0, 0);
			
			Double latitude = currentLocation.getLatitude()*1E6;
		    Double longitude = currentLocation.getLongitude()*1E6;

		    GeoPoint locationPoint = new GeoPoint(latitude.intValue(),longitude.intValue());
		    return locationPoint;
	}
	
	public LocationProvider(Context applicationContext, IUserLocationUpdater updateListener) {
		this.updateListener = updateListener;
		Config config = Config.getInstance();
		
		locationManager = (LocationManager) applicationContext.getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, config.locationUpdateMinTime, config.locationUpdateMinDist, this);
	}

	@Override
	public void onLocationChanged(Location newLocation) {
		currentLocation = newLocation;
		updateListener.updateMyLocation(getMyLocation());
	}

	@Override
	public void onProviderDisabled(String arg0) {
		currentLocation = null;
		
	}

	@Override
	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		// TODO Auto-generated method stub
		
	}
}
